package com.iary.interfaces.taglib;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import com.iary.application.TaokeService;
import com.iary.application.TbShopService;
import com.iary.domain.model.block.Block;
import com.iary.domain.model.block.BlockType;
import com.iary.domain.service.BlockService;
import com.taobao.api.domain.Shop;
import com.taobao.api.domain.TaobaokeItem;

public class BlockTag extends TagSupport {

	private static final long serialVersionUID = 4048797433083338215L;
	
	private static final Logger log = LoggerFactory
	.getLogger(BlockTag.class);
	
	private String type;

	public int doStartTag() throws JspException {
		if(type==null){
			return SKIP_BODY;
		}
		ServletContext servletContext = this.pageContext.getServletContext();
		Object ob = servletContext
		.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		WebApplicationContext context = (WebApplicationContext) ob;
		
		BlockService blockService = context.getBean(BlockService.class);
		TaokeService taokeService = context.getBean(TaokeService.class);
		TbShopService tbShopService = context.getBean(TbShopService.class);
		
		List<Block> list = blockService.getBlocksByType(BlockType.valueOf(type));
		StringBuffer str = new StringBuffer();
		if(BlockType.hotShop.toString().equals(type) && list!=null){
			for (Block block : list) {
				Shop shop = tbShopService.getShopByNick(block.getBlockDesc().getValue());
				if(shop!=null){
					str.append("<li>");
					str.append("<div class=\"pic\">");
					str.append("<img height=\"55\" width=\"55\" alt=\"\" src=\"http://logo.taobao.com/shop-logo").append(shop.getPicPath()).append("\">");
					str.append("</div>");
					str.append("<div class=\"info\">");
					str.append("<p><a href=\"").append("/shop/").append(shop.getNick()).append("\">");
					str.append("<b>").append(shop.getNick()).append("</b></a></p>");
					str.append("<p><a href=\"").append("/shop/").append(shop.getNick()).append("\">");
					str.append(shop.getTitle()).append("</a></p>");
					str.append("<p>").append(shop.getShopScore()).append("</p>");
		     		str.append("</div>");
		     		str.append("</li>");
				}
			}
		}
		if(BlockType.hotItem.toString().equals(type) && list!=null){
			int i = 0;
			str.append("<table border=\"0\" width=\"100%\">");
			str.append("<tr>");
			for (Block block : list) {
				String mock = "<table cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\" style=\"border: 1px solid #E6E6E6;\"><tr><td align=\"center\"><div style=\"margin:5px; width: 160px;height:160px;\"><a target=\"_blank\" href=\"$url$\" style=\"width: 80px; margin:0px;padding:0px;height: 80px; overflow:hidden;\"><img style=\"margin:0px;border:none;\" src=\"$img$\"></a></div><div class=\"clearing\"></div></td></tr><tr><td style=\"height:40px;\"><a target=\"_blank\" href=\"$url$\" style=\"padding-left:5px;margin:5px;line-height:20px;color:#0000FF\">$title$</a></td></tr><tr><td style=\"line-height:22px;padding:0px 0px 8px 0px;\" align=\"center\"><span style=\"margin:5px;line-height:22px;color:#CC0000;\">$price$Ԫ</span></td></tr><tr><td style=\"line-height:22px;padding:0px 0px 8px 0px;\" align=\"center\"><a target=\"_blank\" href=\"$url$\" style=\"float:none;\" ></a></td></tr></table>";
				i++;
				TaobaokeItem item = taokeService.getTbkItem(Long.valueOf(block.getBlockDesc().getValue()));
				str.append("<td>");
				if(item!=null){
					mock = StringUtils.replace(mock, "$url$", "/item/"+item.getNumIid());
					mock = StringUtils.replace(mock, "$img$", item.getPicUrl()+"_160x160.jpg");
					mock = StringUtils.replace(mock, "$title$", item.getTitle());
					mock = StringUtils.replace(mock, "$price$", item.getPrice());
				}
				str.append(mock);
				if(i%4==0 && i!=list.size()){
					str.append("</tr><tr>");
				}
				str.append("</td>");
			}
			str.append("</tr>");
			str.append("</table>");
		}
		
		try {
			pageContext.getOut().write(str.toString());
		} catch (IOException e) {
			log.warn(e.getMessage());
		}
		
		return EVAL_PAGE;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
